//
//  MEDViewController.m
//  idoctor
//
//  Created by Mr Lee on 2017/6/1.
//  Copyright © 2017年 Mr Lee. All rights reserved.
//


#define medKindList @"http://www.tngou.net/api/drug/classify"
#define search @"http://www.tngou.net/api/drug/name?"

#import "MEDViewController.h"
#import "kindModel.h"
#import "MedKindTableViewCell.h"
#import "MedKindListViewController.h"
#import "SearchRstViewController.h"

@interface MEDViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>
{

    NSMutableDictionary *_searchDict;

}

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataArr;
@property (weak, nonatomic) IBOutlet UIView *searchView;

@end

@implementation MEDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
    
    [self loadKind];
}

-(void)initUI
{
    
    [self initTableView];
    
}

-(void)initTableView
{
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    [_tableView registerNib:[UINib nibWithNibName:@"MedKindTableViewCell" bundle:nil]
     forCellReuseIdentifier:@"MedKindTableViewCell"];
    
     UISearchBar * searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)] ;
    searchBar.placeholder = @"请输入药品名";
    
    searchBar.delegate = self;
    
    _tableView.tableHeaderView = searchBar;
    
}

-(void)loadKind
{
    
    [SVProgressHUD showWithStatus:@"加载中"];
    
    _dataArr = [NSMutableArray array];

    [[HttpRequest shardWebUtil]getNetworkRequestURLString:medKindList parameters:nil success:^(id obj) {
        
        //DLog(@"成功的数据---%@",obj);
        
        BOOL status = obj[@"status"];
        if (status) {
        
            [SVProgressHUD dismiss];
            
            for (NSDictionary *dict in obj[@"tngou"]) {
                
                kindModel *model = [[kindModel alloc]initWithDictionary:dict error:nil];
                
                //DLog(@"单个模型----%@",model);
                
                [_dataArr addObject:model];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [_tableView reloadData];
            });
            //DLog(@"模型的数量---%ld",_dataArr.count);
        }

    } fail:^(NSError *error) {
        
    }];

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return  _dataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 65;
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MedKindTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MedKindTableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.model = _dataArr[indexPath.row];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    kindModel *model = _dataArr[indexPath.row];
    
    MedKindListViewController *kindListVc = [[MedKindListViewController alloc]init];
    kindListVc.kindId = model.kindId;
    
    [self.navigationController pushViewController:kindListVc animated:YES];
    
    
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
   

    [self.view endEditing:YES];
    searchBar.placeholder =@"";
    
    NSString *text = searchBar.text;
    
    text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    if (text) {
        
        _searchDict = [NSMutableDictionary dictionary];
        
        [_searchDict setObject:@"吉松" forKey:@"name"];
        
        [self searchMed];
        
    }

}

-(void)searchMed
{
    [SVProgressHUD showWithStatus:@"搜索中"];

    [[HttpRequest shardWebUtil]getNetworkRequestURLString:search parameters:_searchDict success:^(id obj)
    {
        
        DLog(@"字典----%@",_searchDict);
        
        DLog(@"成功的数据---%@",obj);
        
        NSString *status = obj[@"msg"];
       
    
        if(status!=nil){
        
            [SVProgressHUD showErrorWithStatus:@"没有搜到该药品"];
            
        }else{
        
            SearchRstViewController *searchrRstVc = [[SearchRstViewController alloc]init];
            
            searchrRstVc.imagStr = obj[@"img"];
            searchrRstVc.htmlStr = obj[@"message"];
            
            [self.navigationController pushViewController:searchrRstVc animated:YES];
            
        }
        
    } fail:^(NSError *error) {
        
        DLog(@"失败的原因--%@",error);
    }];
    

}

-(void)viewWillDisappear:(BOOL)animated
{
    [SVProgressHUD dismiss];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
